'''
Author: '遂季琉璃' 1846621493@qq.com
Date: 2025-07-29 20:43:09
LastEditors: '遂季琉璃' 1846621493@qq.com
LastEditTime: 2025-10-21 23:30:23
FilePath: \jqktrader-main\构建远端服务.py
Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
'''
import jqktrader
from jqktrader.log import logger
user = jqktrader.use()
user.connect(
  exe_path=r'C:\同花顺软件\同花顺\xiadan.exe',
  tesseract_cmd=r'C:\Program Files\Tesseract-OCR\tesseract.exe'
)

import flask
import json
app = flask.Flask(__name__)

# 获取资金状况
@app.route('/balance', methods=['GET'])
def balance():
    """获取资金信息"""
    logger.info("获取资金信息")
    result = user.balance
    logger.info("返回：{}".format(json.dumps(result, ensure_ascii=False)))
    return flask.jsonify(result)

# 获取持仓
@app.route('/position', methods=['GET'])
def position():
    """获取持仓信息"""
    logger.info("获取持仓信息")
    result = user.position
    logger.info("返回：{}".format(json.dumps(result, ensure_ascii=False)))
    return flask.jsonify(result)

# 获取当日委托
@app.route('/today_entrusts', methods=['GET'])
def today_entrusts():
    """获取当日委托信息"""
    logger.info("获取当日委托信息")
    result = user.today_entrusts
    logger.info("返回：{}".format(json.dumps(result, ensure_ascii=False)))
    return flask.jsonify(result)

# 获取当日成交
@app.route('/today_trades', methods=['GET'])
def today_trades():
    """获取当日成交信息"""
    logger.info("获取当日成交信息")
    result = user.today_trades
    logger.info("返回：{}".format(json.dumps(result, ensure_ascii=False)))
    return flask.jsonify(result)

# 获取可撤单列表
@app.route('/cancel_entrusts', methods=['GET'])
def cancel_entrusts():
    """获取可撤单委托列表"""
    logger.info("获取可撤单列表")
    result = user.cancel_entrusts
    logger.info("返回：{}".format(json.dumps(result, ensure_ascii=False)))
    return flask.jsonify(result)

# 撤单
@app.route('/cancel', methods=['POST'])
def cancel():
    """撤销指定委托单"""
    logger.info("撤销指定委托单")
    entrust_no = flask.request.form.get('entrust_no')
    logger.info("撤单请求：{}".format(json.dumps({'entrust_no': entrust_no}, ensure_ascii=False)))
    result = user.cancel_entrust(entrust_no)
    logger.info("撤单结果：{}".format(json.dumps(result, ensure_ascii=False)))
    return flask.jsonify(result)

# 批量撤单
@app.route('/cancel_all', methods=['POST'])
def cancel_all():
    """批量撤销所有可撤单委托"""
    logger.info("flask收到批量撤单请求")
    user.cancel_all_entrusts()
    return flask.jsonify({'message': '已执行批量撤单'})

# 买入
@app.route('/buy', methods=['POST'])
def buy():
    """买入股票"""
    logger.info("买入股票")
    code = flask.request.form.get('code')
    price = flask.request.form.get('price')
    amount = flask.request.form.get('amount')
    logger.info("买入请求：{}".format(json.dumps({'code': code, 'price': price, 'amount': amount}, ensure_ascii=False)))
    result = user.buy(code, price, amount)
    logger.info("买入结果：{}".format(json.dumps(result, ensure_ascii=False)))
    return flask.jsonify(result)   

# 卖出
@app.route('/sell', methods=['POST'])
def sell():
    """卖出股票"""
    logger.info("卖出股票")
    code = flask.request.form.get('code')
    price = flask.request.form.get('price')
    amount = flask.request.form.get('amount')
    logger.info("卖出请求：{}".format(json.dumps({'code': code, 'price': price, 'amount': amount}, ensure_ascii=False)))
    result = user.sell(code, price, amount)
    logger.info("卖出结果：{}".format(json.dumps(result, ensure_ascii=False)))
    return flask.jsonify(result)

# 市价买入
@app.route('/market_buy', methods=['POST'])
def market_buy():
    """市价买入股票"""
    logger.info("市价买入股票")
    code = flask.request.form.get('code')
    amount = flask.request.form.get('amount')
    logger.info("市价买入请求：{}".format(json.dumps({'code': code, 'amount': amount}, ensure_ascii=False)))
    result = user.market_buy(code, amount, ttype='本方最优')
    logger.info("市价买入结果：{}".format(json.dumps(result, ensure_ascii=False)))
    return flask.jsonify(result)

# 市价卖出
@app.route('/market_sell', methods=['POST'])
def market_sell():
    """市价卖出股票"""
    logger.info("市价卖出股票")
    code = flask.request.form.get('code')
    amount = flask.request.form.get('amount')
    logger.info("市价卖出请求：{}".format(json.dumps({'code': code, 'amount': amount}, ensure_ascii=False)))
    result = user.market_sell(code, amount, ttype='本方最优')
    logger.info("市价卖出结果：{}".format(json.dumps(result, ensure_ascii=False)))
    return flask.jsonify(result)

# 心跳检查
@app.route('/heartbeat', methods=['POST'])
def heartbeat():
    """心跳检查"""
    # logger.info("心跳检查")
    return flask.jsonify({'message': '连接检查成功'})

# 刷新数据
@app.route('/refresh', methods=['POST'])
def refresh():
    """刷新客户端数据"""
    logger.info("刷新客户端数据")
    user.refresh()
    return flask.jsonify({'message': '刷新成功'})

# 退出客户端
@app.route('/exit', methods=['POST'])
def exit_client():
    """退出客户端"""
    logger.info("退出客户端")
    user.exit()
    return flask.jsonify({'message': '客户端已退出'})



if __name__ == '__main__':
  app.run(host='0.0.0.0', port=6030)



# 单元测试
# curl -X POST -d "code=000878&price=12.45&amount=100" http://192.168.5.7:6030/buy
# curl -X POST -d "entrust_no=4723953366" http://192.168.5.7:6030/cancel
